// miniprogram/pages/index/index.js
// import regeneratorRuntime from '../../utils/runtime.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.test()
    // this.testPromise()
    // this.foo()
    this.movie()
    this.music()
  },

  test(){
    const name = 'aqie'
    const person = {
      name,
      age: 24
    }
    console.log(person)
    wx.cloud.callFunction({
      name: 'login'
    }).then((res) => {
      console.log(res)
      this.setData({
        openId: res.result.openid
      })
    })

  },

  testPromise(){
    new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log(1)
        resolve()
      }, 1000)
    }).then((res) => {
      setTimeout(() => {
        console.log(2)
      }, 1000)
    })

    let p1 = new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('p1')
        reject('p1 fali')
      }, 1000)
    })

    let p2 = new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('p2')
        resolve('p2 success')
      }, 1000)
    })

    let p3 = new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('p3')
        resolve('p3 success')
      }, 1000)
    })
    // 全部完成 Promise.race(判断请求超时)
    Promise.all([p1,p2,p3]).then((res) => {
      console.log('任务全部完成')
      console.log(res)
    }).catch((err) => {
      console.log('失败')
    })
  },
  async foo() {
    console.log('foo')
    // 获取异步函数timeout 返回值
    let res = await this.timeout()
    console.log(res)
    // return 1
  },
  timeout() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('123')
        resolve('success')
      }, 1000)
    })
  },
  music(){
    wx.cloud.callFunction({
      name: 'tcbRouter',
      data: {
        $url: 'music'
      }
    }).then((res) => {
      console.log(res)
    })
  },
  movie(){
    wx.cloud.callFunction({
      name: 'tcbRouter',
      data: {
        $url: 'movie'
      }
    }).then((res) => {
      console.log(res)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})